package com.ktjy.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.crm.entity.User;
import com.ktjy.crm.mapper.UserMapper;
import com.ktjy.crm.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        User user=userMapper.getUserByNameWithRoleName(username);
        if (user!=null){
            if (user.getUsrPassword().equals(password)){
                return user;
            }
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUser(Long id) {
        return userMapper.deleteById(id);
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public IPage<User> getUserPage(HashMap<String, Object> hashMap) {
        Integer pageNo= (Integer) hashMap.get("pageIndex");
        Integer pageSize= (Integer) hashMap.get("pageSize");
        Page<User> page=new Page<>(pageNo,pageSize);
        IPage<User> userIPage=userMapper.getUserByPage(page,hashMap);
        return userIPage;
    }
}
